/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wdm.parsers;

import au.com.bytecode.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.wdm.beans.Bus;
import org.wdm.beans.BusStop;
import org.wdm.beans.Route;
import org.wdm.beans.points.Point;

/**
 *
 * @author thanasis
 */
public class TransportParser {

    public static List<Route> parse(Point start, Point end) {
        try {
            HashMap<String, BusStop> busStopMap = new HashMap<String, BusStop>();
            HashMap<String, Bus> busMap = new HashMap<String, Bus>();
            CSVReader reader = new CSVReader(new FileReader("/home/santaris/Desktop/streamNEW.csv"));

            reader.readNext();
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                String route = nextLine[0];
                String direction = nextLine[1];
                String naptan = nextLine[2];
                String name = nextLine[3];
                String latitude = nextLine[4];
                String longitute = nextLine[5];
                if (!busStopMap.containsKey(naptan)) {
                    BusStop busStop = new BusStop();
                    busStop.setNaptan(naptan);
                    busStop.setName(name);
                    Point point = new Point();
                    point.setLatitude(Double.parseDouble(latitude));
                    point.setLongitude(Double.parseDouble(longitute));
                    busStop.setPoint(point);
                    busStopMap.put(naptan, busStop);
                }
                Bus bus = null;
                if (!busMap.containsKey(route)) {
                    bus = new Bus();
                    bus.setRoute(route);
                    busMap.put(route, bus);
                } else {
                    bus = busMap.get(route);
                }
                if (direction.equals("1")) {
                    bus.addToGoing(busStopMap.get(naptan));
                } else {
                    bus.addToReturning(busStopMap.get(naptan));
                }
                busStopMap.get(naptan).addBus(bus);
            }

            
            List<Route> routesList = RouteCalculation.calculateRoute(busStopMap, busMap, start, end, 0.1);
            return routesList;
            
        } catch (IOException ex) {
            Logger.getLogger(TransportRoute.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
}
